package cs.xwi7xwa.activities;

import java.io.IOException;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import cs.xwi7xwa.R;
import cs.xwi7xwa.data.ExhibitDbAdapter;
import cs.xwi7xwa.data.FactDbAdapter;
import cs.xwi7xwa.templates.BumpTemplate;

public class FactChartView extends BumpTemplate {

    private String name;
    private int id;
    private FactDbAdapter factDbAdapter;
    private Cursor cursor;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exhibit_factchart);

        // Get all extra data that came with the Intent
        Bundle extras = getIntent().getExtras();

        if (extras != null) {
            name = extras.getString(ExhibitDbAdapter.KEY_NAME);
            id = extras.getInt(ExhibitDbAdapter.KEY_ID, -1);
        }

        if (name != null) {
            ((TextView) findViewById(R.id.factChartTitle)).setText(name);
        }

        // Load the database
        factDbAdapter = new FactDbAdapter(this);

        try {
            factDbAdapter.open();
        } catch (IOException e) {
            e.printStackTrace();
        }

        cursor = factDbAdapter.fetchAll(id);
        startManagingCursor(cursor);

        if (id != -1 && cursor.moveToFirst()) {
            int ii = 1;
            do {
                if (cursor.getInt(cursor
                    .getColumnIndexOrThrow(FactDbAdapter.KEY_ISHIDDEN)) == 0) {
                    int idAttr = -1;

                    switch (ii) {
                    case 1:
                        idAttr = R.id.Fact1;
                        break;
                    case 2:
                        idAttr = R.id.Fact2;
                        break;
                    case 3:
                        idAttr = R.id.Fact3;
                        break;
                    case 4:
                        idAttr = R.id.Fact4;
                        break;
                    case 5:
                        idAttr = R.id.Fact5;
                        break;
                    }

                    if (idAttr != -1) {
                        ((ImageButton) findViewById(idAttr)).setVisibility(1);
                    }
                }
            } while (ii++ <= 5 && cursor.moveToNext());
        }
    }

    public void goToFact(View view) {
        int factNumber = 0;
        System.out.println(view.getId());
        switch (view.getId()) {
        case R.id.Fact1:
            factNumber = 1;
            break;
        case R.id.Fact2:
            factNumber = 2;
            break;
        case R.id.Fact3:
            factNumber = 3;
            break;
        case R.id.Fact4:
            factNumber = 4;
            break;
        case R.id.Fact5:
            factNumber = 5;
            break;
        }

        if (cursor.moveToPosition(factNumber - 1)) {
            Intent intent = new Intent(view.getContext(), FactView.class);
            intent.putExtra(FactDbAdapter.KEY_ID, cursor.getInt(cursor
                .getColumnIndexOrThrow(FactDbAdapter.KEY_ID)));
            intent.putExtra(FactDbAdapter.KEY_DESC, cursor.getString(cursor
                .getColumnIndexOrThrow(FactDbAdapter.KEY_DESC)));
            intent.putExtra(FactView.FACT_NUMBER, factNumber);
            startActivityForResult(intent, 0);
        } else {
            Toast.makeText(this, "Sorry, this fact does not exist yet",
                Toast.LENGTH_SHORT).show();
        }
    }
}
